package com.koushikdutta.ion;

import com.example.ohosasynclibrary.async.ByteBufferList;
import com.example.ohosasynclibrary.async.future.FutureCallback;
import com.example.ohosasynclibrary.async.util.TextUtils;
import com.koushikdutta.ion.bitmap.BitmapInfo;
import com.koushikdutta.ion.bitmap.IonBitmapCache;
import com.koushikdutta.ion.gif.GifDecoder;
import com.koushikdutta.ion.gif.GifFrame;
import ohos.agp.utils.Point;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;

import java.nio.ByteBuffer;

class LoadBitmap extends LoadBitmapEmitter implements FutureCallback<Response<ByteBufferList>> {
    int resizeWidth;
    int resizeHeight;

    public LoadBitmap(Ion ion, String urlKey, boolean put, int resizeWidth, int resizeHeight, boolean animateGif) {
        super(ion, urlKey, put, animateGif);
        this.resizeWidth = resizeWidth;
        this.resizeHeight = resizeHeight;
    }

    @Override
    public void onCompleted(Exception e, final Response<ByteBufferList> response) {
        if (e == null)
            e = response.getException();
        if (e != null) {
            report(e, null);
            return;
        }
        final ByteBufferList result = response.getResult();

        if (ion.bitmapsPending.tag(key) != this) {
            result.recycle();
            return;
        }

        Ion.getBitmapLoadExecutorService().execute(new Runnable() {
            @Override
            public void run() {
                if (ion.bitmapsPending.tag(key) != LoadBitmap.this) {
                    result.recycle();
                    return;
                }

                ByteBuffer bb = null;
                PixelMap bitmap = null;
                try {
                    bb = result.getAll();


                    GifDecoder gifDecoder;

                    String mimeType = "image/*";
//                    ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
//                    sourceOptions.formatHint = mimeType;
//                    ImageSource imageSource = ImageSource.create(bb.array(), sourceOptions);

                    LogUtils.info("config ImageSource.DecodingOptions_0078___" + resizeWidth + "---" + resizeHeight);
                    ImageSource.DecodingOptions options = new ImageSource.DecodingOptions();
//                            ion.bitmapCache.prepareBitmapOptions(imageSource.getImageInfo().size, resizeWidth, resizeHeight);
                    LogUtils.info("config ImageSource.DecodingOptions_0075");
                    final Point size = new Point(resizeWidth, resizeHeight);
                    if (animateGif && TextUtils.equals("image/gif", mimeType)) {
//                        new GifDecoder(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining());
                        gifDecoder = new GifDecoder(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining());
                        GifFrame frame = gifDecoder.nextFrame();
                        bitmap = frame.image;
                        // the byte buffer is needed by the decoder
                        bb = null;
                    } else {
                        bitmap = IonBitmapCache.loadBitmap(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining(), options);
                        gifDecoder = null;
                        if (bitmap == null)
                            throw new Exception("failed to load bitmap");
                    }

                    BitmapInfo info = new BitmapInfo(key, mimeType, bitmap, size);
                    info.gifDecoder = gifDecoder;
                    info.servedFrom = response.getServedFrom();
                    report(null, info);
                } catch (OutOfMemoryError e) {
                    report(new Exception(e), null);
                    LogUtils.info("Exception e_0076" + e.getMessage());
                } catch (Exception e) {
                    report(e, null);
                    LogUtils.info("Exception e_0077" + e.getMessage());
                } finally {
                    ByteBufferList.reclaim(bb);
                }
            }
        });
    }
}
